package com.yy.driver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.driver.pojo.Driver;
import com.yy.driver.mapper.DriverMapper;
import org.springframework.stereotype.Service;
import com.yy.driver.service.DriverService;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 驾驶员信息表(Driver)表服务实现类
 *
 * @author yan yi
 * @since 2021-11-27 14:54:51
 * @version 1.0
 */
@Service("driverService")
public class DriverServiceImpl implements DriverService {
    @Resource
    private DriverMapper driverMapper;

    /**
     * 查询分页数据
     * @param page 查询起始位置
     * @param name 查询条数
     * @return 对象列表
     */
	@Override
    public Map<String, Object> chaXunFenYe(int page, String name,int sex) {
    // 获取当前表中的总记录
        int tableCount = this.driverMapper.chaXunCount(name,sex);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / 10 + 1;
        // 计算每页开始的下标值
        int xiaBiao = (page - 1) * 10;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.driverMapper.chaXunFenYe(xiaBiao, name,sex));
        return map;
    }

    /**
     * 新增数据
     * @param driver 实例对象
     * @return 实例对象
     */
	@Override
    public Map<String, Object> xinZeng(Driver driver) {
        // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
        driver.setCheckintime(new Date());
        driver.setIsdelete(0);
        driver.setAltertime(new Date());

        this.driverMapper.xinZeng(driver);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID更新单条数据
     * @param driver 实例对象
     * @return 实例对象
     */
	@Override
    public Map<String, Object> gengXinById(Driver driver) {
        this.driverMapper.gengXinById(driver);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     * @param driverid 主键
     * @return 是否成功
     */
	@Override
    public Map<String, Object> shanChuById(String driverid) {
        this.driverMapper.shanChuById(driverid);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }

    //集成PageHelper实现分页查询
    @Override
    public Map<String, Object> findByPage(int pageNum,int pageSize,String name,int sex){
        PageHelper.startPage(pageNum,pageSize);
        //这两行代码间建议不要放其他操作数据库代码
        List<Driver> drivers = this.driverMapper.chaXunAll(name,sex);
        PageInfo<Driver> pageInfo = new PageInfo<>(drivers);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "分页查询成功");
        map.put("data",pageInfo);
        return map;
    }
}